import { Component, OnInit } from '@angular/core';
import { Book } from '../../model/book';
import { CourseService } from '../../service/course.service';

@Component({
  selector: 'app-book',
  templateUrl: './book.component.html',
  styleUrls: ['./book.component.css']
})
export class BookComponent implements OnInit {

  b:Book
  bl:Array<Book>

  constructor(private courseservice:CourseService) { 
    this.b=new Book();
  }

  ngOnInit() {
    this.loadBookList();

  }

  save(){
    if(this.b.id){
      this.update()
    }else{
  this.addBook();
  }
}

  addBook(){
    this.courseservice.addBook(this.b)
    .then((flag:boolean)=>{
      if(flag==true){
        this.b=new Book();
        this.loadBookList();
      }else{
        alert("添加异常，请重试")
      }
    })
  }

  loadBookList(){
    this.bl=new Array();

    this.courseservice.getBookList()
    .then((data:any)=>{
      this.bl=data;
    })
  }

  toAdd(){
    this.b=new Book();
  }

  toupdate(x:Book){
    this.b=x;
  }

  update(){
    this.courseservice.updateBook(this.b)
    .then((flag:boolean)=>{
      if(flag==true){
        this.b=new Book();
        this.loadBookList();
      }else{
        alert("修改异常，请重试");
      }
    })
  }

  deleteBook(x:Book){
    this.courseservice.deleteBook(x)
    .then((flag:boolean)=>{
     if(flag == true){
       this.loadBookList();
     }
     else{
 
       alert("删除异常，请重试");
     }
 
    })
 
   }


}


